<?php
App::uses('AppModel', 'Model');
App::uses('BrValidation', 'Localized.Validation');

/**
 * Cliente Model
 *
 * @property TipoCliente $TipoCliente
 * @property Endereco $Endereco
 * @property EstadoCivil $EstadoCivil
 * @property Pedido $Pedido
 */
class Cliente extends AppModel {

/**
 * Display field
 *
 * @var string
 */
	var $name = 'Cliente';
	public $displayField = 'nome';
	
	
	public function beforeSave($options = array()){
	
		$registros = $this->find('count', array('conditions'=> array('Cliente.cpf' => $this->request->data['Cliente']['cpf'])));
		if($registros>0){
			return false;
		}
		else
			return true;
	}

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'nome' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Campo não pode ser vazio.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			)
		),
		'cpf' => array(
			 'valid' => array(
				'rule' => array('ssn', null, 'br'),
				'message' => 'Digite um CPF válido!',
				'allowEmpty' => false,
				'required' => true,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			), 
		),
		'rg' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'message' => 'Tipo n�o permitido',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
			
		),
		'data_nasc' => array(
 			'date' => array(
 				'rule' => array('date'),
 				'message' => 'Formato n�o permitido',
 				//'allowEmpty' => false,
 				//'required' => false,
 				//'last' => false, // Stop validation after this rule
 				//'on' => 'create', // Limit validation to 'create' or 'update' operations/ 			),
			)
		),
		'sexo' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'email' => array(
			'email' => array(
				'rule' => array('email'),
				'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
 		'telefone' => array(
 			'valid' => array(
 				'rule' => array('phone', null, 'br'),
 				'message' => 'Digite o n�mero do telefone corretamente!',
 				'allowEmpty' => false,
 				'required' => true,
 				//'last' => false, // Stop validation after this rule
 				//'on' => 'create', // Limit validation to 'create' or 'update' operations
 			),
 		),
		'celular' => array(
			'valid' => array(
				'rule' => array('phone', null, 'br'),
				'message' => 'Digite apenas o numero do telefone.',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'TipoCliente' => array(
			'className' => 'TipoCliente',
			'foreignKey' => 'tipo_cliente_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Endereco' => array(
			'className' => 'Endereco',
			'foreignKey' => 'endereco_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'EstadoCivil' => array(
			'className' => 'EstadoCivil',
			'foreignKey' => 'estado_civil_id',
			'conditions' => '',
			'fields' => 'descricao',
			'order' => ''
		)
	);

/**
 * hasMany associations
 *
 * @var array
 */
	public $hasMany = array(
		'Pedido' => array(
			'className' => 'Pedido',
			'foreignKey' => 'cliente_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);

}
